//
//  KZHotSearchingView.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/26.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

class KZSearchItem: KZBaseModel {
    
    var id: Int = 0                 // 图片显示
    var content: String = ""        // 标题
    
    
    enum CodingKeys: String, CodingKey {
        case id
        case content
    }
}

class KZHotSearchingView: UIView {
    
    var clickClouse: (String) -> Void = {_ in}
    
    let listView = UIView()
    
    var list: [KZSearchItem] = [KZSearchItem]() {
        didSet {
            self.setNeedsLayout()
        }
    }

    convenience init() {
        
        self.init(frame: .zero)
        
        setupUI()
    }
    
    

}

extension KZHotSearchingView {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard list.count > 0 else {return}
        
        for subview in listView.subviews {
            subview.removeFromSuperview()
        }
        
        var last: UIView? = nil
        for (i, item) in list.enumerated() {
            
            let button = UIButton(type: .custom)
            button.addTarget(self, action: #selector(onButtonClick(button:)), for: .touchUpInside)
            button.titleLabel?.font = kFont(14)
            button.layer.cornerRadius = 2
            button.layer.masksToBounds = true
            button.backgroundColor = UIColor.white
            button.setTitle(item.content, for: .normal)
            button.setTitleColor(UIColor.lightGray, for: .normal)
            listView.addSubview(button)
            button.snp.makeConstraints { (m) in
                m.width.equalTo(W(76))
                m.height.equalTo(button.snp.width).multipliedBy(54.0 / 152.0)
                if let last = last {
                    if i % 3 != 0 {
                        m.left.equalTo(last.snp.right).offset(W(26))
                    } else {
                        m.left.equalTo(W(95.0/2))
                    }
                    if i / 3 != 0 {
                        m.top.equalTo(last.snp.bottom).offset(14)
                    } else {
                        m.top.equalTo(0)
                    }
                } else {
                    m.left.equalTo(W(95.0/2))
                    m.top.equalTo(0)
                }
            }
            
            last = button
        }
        
        last!.snp.makeConstraints { (m) in
            m.bottom.equalTo(0)
        }
    }
    
    func setupUI() {
        
        let titleLabel = UILabel()
        titleLabel.textColor = UIColor.lightGray
        titleLabel.textAlignment = .left
        titleLabel.text = "热门搜索"
        titleLabel.font = UIFont.systemFont(ofSize: 15)
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (m) in
            m.left.equalTo(20)
            m.top.equalTo(0)
        }
        
        self.addSubview(listView)
        listView.snp.makeConstraints { (m) in
            m.left.right.equalTo(0)
            m.bottom.equalTo(0)
            m.top.equalTo(titleLabel.snp.bottom).offset(30)
        }
        
    }
}

extension KZHotSearchingView {
    
    @objc func onButtonClick(button: UIButton) {
        clickClouse(button.titleLabel!.text!)
    }
}
